<template>
  <div class="selector-wrap">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.keywords"
            placeholder="关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="filteredMaterials" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="!operation" />
      <el-table-column label="名称" prop="name" width="150" />
      <el-table-column label="主材种类数" prop="mainMaterialNum" width="150" />
      <el-table-column label="辅材种类数" prop="subMaterialNum" width="150" />
      <el-table-column label="定额种类数" prop="normMaterialNum" width="150" />
      <el-table-column label="标注价格" prop="price" width="150">
        <template #default="scope">
          {{ scope.row.price + "元" }}
        </template>
      </el-table-column>
      <el-table-column label="总成本价" prop="totalCostPrice" width="150">
        <template #default="scope">
          {{ scope.row.totalCostPrice + "元" }}
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" min-width="150" prop="remark" />
      <el-table-column fixed="right" align="center" label="操作" width="100" v-if="operation">
        <template #default="scope">
          <el-button
            v-if="operation == 'add'"
            type="primary"
            size="small"
            icon="plus"
            @click="handleAdd(scope.row)"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-if="total > 0"
      v-model:total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from "vue";
import SealMealAPI, {
  SealMealPageVO,
  SealMealForm,
  SealMealPageQuery,
} from "@/api/business/material/sealMeal.api";

const props = defineProps({
  type: {
    type: String,
    required: false,
  },
  modelValue: {
    type: Array,
    default: () => [],
  },
  exQuery: {
    type: Object,
    default: () => ({}),
  },
  // 操作类型 默认 无-勾选 add-添加数量
  operation: {
    type: String,
    default: "",
  },
});
const queryFormRef = ref();
const queryParams = reactive<SealMealPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const emit = defineEmits(["update:modelValue", "selection-change", "add-item"]);

const checked = ref<SealMealPageVO[]>([]);
const handleSelectionChange = (selection: SealMealPageVO[]) => {
  checked.value = selection;
  emit("update:modelValue", selection);
  emit("selection-change", selection);
};

// 添加按钮
const handleAdd = (row: SealMealPageVO) => {
  ElMessage.success("添加成功");
  emit("add-item", row);
};

function getChecked(): SealMealPageVO[] {
  return checked.value || [];
}

const total = ref(0);
const filteredMaterials = ref<SealMealPageVO[]>();

const handleQuery = () => {
  Object.assign(queryParams, props.exQuery);
  SealMealAPI.getPage(queryParams).then((data) => {
    filteredMaterials.value = data.list;
    total.value = data.total;
  });
};

const handleResetQuery = () => {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
};

defineExpose({
  getChecked,
  handleQuery,
});
</script>

<style scoped>
.selector-wrap {
  padding: 10px;
}
</style>
